const mysql = require('mysql')
const config = require('../../config/dev.conf')

class SqlService{
    constructor() {
        this.config = config.db2
        this.option = {
            host: this.config.host,
            user: this.config.user,
            password: this.config.password,
            database: this.config.database,
            multipleStatements: this.config.multipleStatements
        }
    }

    getConnection() {
        if (this.connection) {
            return this.connection
        } 
        try{
            this.connection = mysql.createConnection(this.option)
        }catch(e){
            console.error('数据库连接失败：',e)
            this.connection = {}
        }
        
        return this.connection

    }

    getPool() { 
        if (this.pool) {
            return this.pool
        }
        try{
            this.pool = mysql.createPool(this.option)
        }catch(e){
            this.pool = {}
        }
       
        return this.pool
    }
}


module.exports = SqlService;
